package com.zretc.users.service;

import com.zretc.api.pojo.User;
import com.zretc.common.constant.WebConstant;
import com.zretc.common.entity.R;
import com.zretc.common.entity.RespResult;
import com.zretc.users.dao.UsersMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

@Service
public class UsersService {
    @Resource
    private UsersMapper userMapper;
    public User getUserById(Integer userId){
        System.out.println("UserService-----------------------------------------------------------------------"); // test
        User user = userMapper.getUserById(userId);
        System.out.println(user); // test
        return user;
    }

    public User getUserByUsername(String userName){
        User user = userMapper.getUserByName(userName);
        System.out.println(user);
        return user;
    }

    public Integer getPoint(Integer userId){
        return userMapper.getPoint(userId);
    }

    public Integer getReputation(Integer userId){
        return userMapper.getReputation(userId);
    }

    public RespResult changePointOrRepu(User user) {
        if (Objects.isNull(user)) {
            return R.fail(WebConstant.NO_USER_ERROR_CODE, WebConstant.ERROR_301);
        } else {
            userMapper.updatePointOrRepu(user);
            return R.success("操作成功");
        }
    }

    public RespResult changeNicknameOrPhone(User user, User newInfo) {
        if(Objects.isNull(user))
        {
            return R.fail(404, WebConstant.ERROR_404);
        }
        else if(/*Objects.isNull(newInfo.getPassword()) || */Objects.isNull(newInfo.getNickName()))
        {
            return R.fail(310, "请输入必要信息" );
        }
        else
        {
            user.setNickName(newInfo.getNickName());
            user.setPhoneNumber(newInfo.getPhoneNumber());
            userMapper.updateNicknameOrPhone(user);
            return R.success("操作成功");
        }
    }
}
